package com.flyco.modules.stm.vo;

import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Locale;

@Component
public class I18nHelper {
    private static MessageSource messageSource;

    public I18nHelper() {
    }

    @Resource
    public void setMessageSource(MessageSource messageSource) {
        I18nHelper.messageSource = messageSource;
    }

    public static MessageSource getMessageSource() {
        return messageSource;
    }

    public static Locale getCurrentLocale() {
        return LocaleContextHolder.getLocale();
    }

    public static String getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException {
        return messageSource.getMessage(resolvable, getCurrentLocale());
    }

    public static String getMessage(String code, Object[] args) throws NoSuchMessageException {
        return messageSource.getMessage(code, args, getCurrentLocale());
    }

    public static String getMessage(String code) throws NoSuchMessageException {
        return messageSource.getMessage(code, (Object[])null, getCurrentLocale());
    }

    public static String getMessage(String code, Object[] args, String defaultMsg) {
        return messageSource.getMessage(code, args, defaultMsg, getCurrentLocale());
    }

    public static String getMessage(String code, String defaultMsg) {
        return getMessage(code, (Object[])null, defaultMsg);
    }
}
